package com.zc.controller;

import com.zc.entity.User;
import com.zc.entity.Userandteam;
import com.zc.mapper.UserandteamMapper;
import com.zc.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.aspectj.lang.annotation.Aspect;
import javax.servlet.http.HttpSession;
import java.util.Map;


@Controller
public class LoginController {

    //    @DeleteMapping
//    @PutMapping
//    @GetMapping
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    UserandteamMapper userandteamMapper;
    @Autowired
    UserService userService;

    //@RequestMapping(value = "/user/login",method = RequestMethod.POST)
    @GetMapping(value = "/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Map<String, Object> map, HttpSession session) {
        User user = userService.userLogin(username);

        if (user == null) {
            logger.info("用户名不存在");
            map.put("msg", "用户名不存在");
            return "login";
        } else if (user != null && user.getPassword().equals(password)) {
            //登陆成功，防止表单重复提交，可以重定向到主页
            session.setAttribute("loginUser", user.getRealname());
            session.setAttribute("userid",user.getId());
            session.setAttribute("grade",user.getGrade());
            logger.info(session.getAttribute("grade").toString());
            Userandteam userandteam=userandteamMapper.selectByPrimaryKey(user.getId());
            if(userandteam!=null)
            {
                session.setAttribute("teamid",userandteam.getTeamid());
            }
            if(user.getGrade()==1)
            {
                return "redirect:/kfmain.html";
            }else if(user.getGrade()==2)
            {
                return "redirect:/csmain.html";
            } else {
                return "redirect:/main.html";
            }

        } else if (user != null && !user.getPassword().equals(password)) {
            logger.info("密码错误");
            map.put("msg", "密码错误");
            return "login";
        }else{
            logger.info("密码错误");
            map.put("msg", "密码错误");
            return "login";
        }

    }
    @GetMapping("user/loginout")
    public String loginOut(HttpSession session)
    {
        session.invalidate();
        return "login";
    }

}
